﻿using Currency.Infra.Repository.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Currency.Entity.Shared
{
    /// <summary>
    /// 实现实体与EF映射的接口
    /// </summary>
    public class EntityInfo : IEntityInfo
    {
        private readonly Assembly _assembly;
        public EntityInfo(Assembly assembly)
        {
            _assembly = assembly;
        }
        protected virtual IEnumerable<Type> GetEntityTypes(Assembly assembly)
        {
            var entityTypes = assembly.GetTypes().Where(m =>
                                                       m.FullName != null
                                                       && m.IsDefined(typeof(EfCoreEntityAttribute), true)
                                                       ).ToArray();

            return entityTypes;
        }

        public virtual (Assembly Assembly, IEnumerable<Type> Types) GetEntitiesInfo()
        {
            var assembly = _assembly;
            var entityTypes = GetEntityTypes(assembly);
            return (assembly, entityTypes);
        }
    }
}
